#include "imageset.h"

#include <stdlib.h>

#include "util.h"

Imageset* imageset_new(char* filename)
{
	Imageset* is = calloc(1, sizeof(Imageset));
	is->filename = filename;
	is->images = NULL;
	utarray_new(is->images, &image_icd);

	// colors
	int i;
	for(i=0;i<254;i++)
		is->color[i].r = is->color[i].g = is->color[i].b = 0;
	is->color[254].r = is->color[254].g = is->color[254].b = 255;
	is->color[255].r = is->color[255].g = is->color[255].b = 128;

	for(i=0;i<255;i++)
		is->color_name[i] = "";
	is->color_name[0] = "Black";
	is->color_name[254] = "White";
	is->color_name[255] = "Transparent";

	return is;
}


Image* imageset_add_image(Imageset* is)
{
	Image* img = malloc(sizeof(Image));
	img->imageset = is;

	img->sf = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY, 
			32, 32, 8, 0, 0, 0, 0);

	if(!img->sf)
		abort();
	SDL_SetColors(img->sf, is->color, 0, 256);
	SDL_SetColorKey(img->sf, SDL_SRCCOLORKEY, 255);
	SDL_FillRect(img->sf, NULL, TRANSPARENT);

	utarray_push_back(is->images, img);
	
	return img;
}


void imageset_delete_image(Imageset* imageset, int n)
{
	Image* img = (Image*)utarray_eltptr(imageset->images, n);
	if(!img)
		abort();
	SDL_FreeSurface(img->sf);
	utarray_erase(imageset->images, n, 1);
}
